package com.winning.ds.role.controller;

import com.winning.ds.common.controller.BaseController;
import com.winning.ds.common.entity.SysGncdEntity;
import com.winning.ds.common.util.Common;
import com.winning.ds.common.util.ResponseEntity;
import com.winning.ds.common.util.UUIDUtils;
import com.winning.ds.role.entity.SysGncdGnanEntity;
import com.winning.ds.role.entity.SysGncdQxanEntity;
import com.winning.ds.role.entity.SysGncdTreeEntity;
import com.winning.ds.role.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: plat
 * @description: 菜单相关controller
 * @author: nxs
 * @create: 2018-07-20 15:23
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

  @Autowired private MenuService mnuService;

  /**
   * 获取菜单树--根据机构编码
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryMenu/{yydm}", method = RequestMethod.GET)
  public ResponseEntity getMenuTree(@PathVariable String yydm) {

    if (!Common.validateParams(yydm)) {
      log.error("错误", "无法获取应用编号");
      return ResponseEntity.error(201, "无法获取应用编号");
    }
    List<SysGncdTreeEntity> list = mnuService.queryMenuByAppid(yydm);
    return ResponseEntity.ok().put("menuTreeList", list);
  }

  @RequestMapping(value = "/queryGncd", method = RequestMethod.POST)
  public ResponseEntity queryGncd(
      HttpServletRequest request, @RequestBody Map<String, Object> queryParams) {

    List<SysGncdTreeEntity> list = mnuService.queryMenuByParams(queryParams); // 没有拼接children的菜单list
    List<SysGncdTreeEntity> tree = mnuService.hasChildGncd(list); // 拼接children的菜单

    List<SysGncdTreeEntity> queryList = list;
    // 当顶级菜单是一个时，说明是顶级的应用名称菜单，不展示，结果中去除
    if (!queryParams.containsKey("parmers")
        || StringUtils.isEmpty(queryParams.get("parmers").toString())) {
      if (tree.size() == 1 && StringUtils.isEmpty(tree.get(0).getpId())) {
        String id = tree.get(0).getId();
        // list中去除
        for (int i = 0; i < queryList.size(); i++) {
          if (id.equals(queryList.get(i).getId())) {
            list.remove(i);
          }
        }
        // tree中去除
        tree = tree.get(0).getChildren();
      }
    }

    ResponseEntity responseEntit = new ResponseEntity();
    responseEntit.put("menuTreeList", tree);
    responseEntit.put("menuList", list);

    return responseEntit;
  }

  /**
   * 保存菜单
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public ResponseEntity saveMenu(@RequestBody SysGncdEntity sge) {
    if (!Common.validateParams(
        sge.getMcaption(),
        sge.getApplicationid(),
        sge.getPcode(),
        sge.getJzfs(),
        sge.getActive())) {
      return ResponseEntity.error(201, "参数为空");
    }
    String cddm = UUIDUtils.getUUID();
    try {
      mnuService.saveMenu(sge, cddm);
    } catch (Exception e) {
      return ResponseEntity.error("保存失败，请联系管理员");
    }

    return ResponseEntity.ok().put("cddm", cddm);
  }

  /**
   * 保存按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/saveGnan", method = RequestMethod.POST)
  public ResponseEntity save(@RequestBody SysGncdGnanEntity sgge) {
    if (!Common.validateParams(sgge.getSjcddm())) {
      return ResponseEntity.error(201, "无法获取上级菜单编码");
    }
    if (!Common.validateParams(sgge.getGnmc())) {
      return ResponseEntity.error(201, "按钮名称不能为空");
    }
    sgge.setMgorder("0");
    mnuService.saveGnan(sgge);
    return ResponseEntity.ok();
  }

  /**
   * 保存数据权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/saveQxan", method = RequestMethod.POST)
  public ResponseEntity saveQxan(@RequestBody SysGncdQxanEntity sgge) {
    if (!Common.validateParams(sgge.getSjsjcddm())) {
      return ResponseEntity.error(201, "无法获取上级菜单编码");
    }
    if (!Common.validateParams(sgge.getSjmc())) {
      return ResponseEntity.error(201, "按钮名称不能为空");
    }
    sgge.setMgorder("0");
    mnuService.saveQxan(sgge);
    return ResponseEntity.ok();
  }

  /**
   * 查询 功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryGnan/{sjcddm}", method = RequestMethod.GET)
  public ResponseEntity queryGnan(@PathVariable String sjcddm) {

    if (!Common.validateParams(sjcddm)) {
      return ResponseEntity.error(201, "无法获取上级菜单编号");
    }
    List<SysGncdGnanEntity> list = mnuService.queryGnan(sjcddm);
    return ResponseEntity.ok().put("gnanList", list);
  }

  /**
   * 查询 权限按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryQxan/{sjsjcddm}", method = RequestMethod.GET)
  public ResponseEntity queryQxan(@PathVariable String sjsjcddm) {

    if (!Common.validateParams(sjsjcddm)) {
      return ResponseEntity.error(201, "无法获取上级菜单编号");
    }
    List<SysGncdQxanEntity> list = mnuService.queryQxan(sjsjcddm);
    return ResponseEntity.ok().put("qxanList", list);
  }

  /**
   * 删除 - 功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/delGnan/{lsh}", method = RequestMethod.DELETE)
  public ResponseEntity delGnan(@PathVariable String lsh) {

    if (!Common.validateParams(lsh)) {
      return ResponseEntity.error(201, "无法获取流水号");
    }
    mnuService.delGnan(lsh);
    return ResponseEntity.ok();
  }

  /**
   * 删除 - 权限按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/delQxan/{sjlsh}", method = RequestMethod.DELETE)
  public ResponseEntity delQxan(@PathVariable String sjlsh) {

    if (!Common.validateParams(sjlsh)) {
      return ResponseEntity.error(201, "无法获取流水号");
    }
    mnuService.delQxan(sjlsh);
    return ResponseEntity.ok();
  }

  /**
   * 查询 功能按钮 -根据流水号
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryGnanBylsh/{lsh}", method = RequestMethod.GET)
  public ResponseEntity queryGnanBylsh(@PathVariable String lsh) {

    if (!Common.validateParams(lsh)) {
      return ResponseEntity.error(201, "无法获取流水号");
    }
    SysGncdGnanEntity sgge = mnuService.queryGnanBylsh(lsh);
    return ResponseEntity.ok().put("SysGncdGnanEntity", sgge);
  }

  /**
   * 查询 功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryGnanByParams", method = RequestMethod.POST)
  public ResponseEntity queryGnanByParams(
      HttpServletRequest request, @RequestParam Map<String, Object> queryParams) {

    queryParams.put("jlzt", "0");
    List<SysGncdGnanEntity> gnanList = mnuService.queryGnanByParams(queryParams);
    return ResponseEntity.ok().put("gnanList", gnanList);
  }

  /**
   * 查询 权限按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryQxanByParams", method = RequestMethod.POST)
  public ResponseEntity queryQxanByParams(
      HttpServletRequest request, @RequestParam Map<String, Object> queryParams) {

    queryParams.put("jlzt", "0");
    List<SysGncdQxanEntity> qxanList = mnuService.queryQxanByParams(queryParams);
    return ResponseEntity.ok().put("qxanList", qxanList);
  }

  /**
   * 查询 权限按钮 -根据流水号
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryQxanBylsh/{sjlsh}", method = RequestMethod.GET)
  public ResponseEntity queryQxanBylsh(@PathVariable String sjlsh) {

    if (!Common.validateParams(sjlsh)) {
      return ResponseEntity.error(201, "无法获取流水号");
    }
    SysGncdQxanEntity sgge = mnuService.queryQxanBylsh(sjlsh);

    return ResponseEntity.ok().put("SysGncdQxanEntity", sgge);
  }

  /**
   * 更新功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateGnan", method = RequestMethod.POST)
  public ResponseEntity upadteGnan(@RequestBody SysGncdGnanEntity sgge) {
    if (!Common.validateParams(sgge.getSjcddm())) {
      return ResponseEntity.error(201, "无法获取上级菜单编码");
    }
    if (!Common.validateParams(sgge.getGnmc())) {
      return ResponseEntity.error(201, "按钮名称不能为空");
    }
    mnuService.updateGnan(sgge);
    return ResponseEntity.ok();
  }

  /**
   * 更新数据权限按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateQxan", method = RequestMethod.POST)
  public ResponseEntity upadteQxan(@RequestBody SysGncdQxanEntity sgge) {
    if (!Common.validateParams(sgge.getSjsjcddm())) {
      return ResponseEntity.error(201, "无法获取上级菜单编码");
    }
    if (!Common.validateParams(sgge.getSjmc())) {
      return ResponseEntity.error(201, "按钮名称不能为空");
    }
    mnuService.updateQxan(sgge);
    return ResponseEntity.ok();
  }

  /**
   * 查询 菜单详情 -根据mcode
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/queryGncdInfo/{mcode}", method = RequestMethod.GET)
  public ResponseEntity queryGncdInfo(@PathVariable String mcode) {

    if (!Common.validateParams(mcode)) {
      return ResponseEntity.error(201, "无法获取菜单编号");
    }
    SysGncdEntity sge = mnuService.queryGncdInfo(mcode);
    return ResponseEntity.ok().put("SysGncdEntity", sge);
  }

  /**
   * 删除 - 菜单
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/delGncd/{mcode}", method = RequestMethod.DELETE)
  public ResponseEntity delGncd(@PathVariable String mcode) {

    if (!Common.validateParams(mcode)) {
      return ResponseEntity.error(201, "无法获取菜单编号");
    }
    mnuService.delGncd(mcode);
    return ResponseEntity.ok();
  }

  /**
   * 更新菜单
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateGncd", method = RequestMethod.POST)
  public ResponseEntity updateGncd(@RequestBody SysGncdEntity sge) {
    if (!Common.validateParams(sge.getMcode())) {
      return ResponseEntity.error(201, "无法获取菜单编码");
    }
    if (!Common.validateParams(sge.getMcaption(), sge.getJzfs(), sge.getActive())) {
      return ResponseEntity.error(201, "表单必填项为空");
    }
    try {
      mnuService.updateGncd(sge);
    } catch (Exception e) {
      return ResponseEntity.error("保存失败，请联系管理员");
    }
    return ResponseEntity.ok();
  }

  /**
   * 批量更新功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateGnanList", method = RequestMethod.POST)
  public ResponseEntity updateGnanList(@RequestBody List<SysGncdGnanEntity> GnanList) {
    for (SysGncdGnanEntity sgge : GnanList) {
      mnuService.updateGnan(sgge);
    }
    return ResponseEntity.ok();
  }
  /**
   * 批量更新功能按钮
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateQxanList", method = RequestMethod.POST)
  public ResponseEntity updateQxanList(@RequestBody List<SysGncdQxanEntity> GnanList) {
    for (SysGncdQxanEntity sgge : GnanList) {
      mnuService.updateQxan(sgge);
    }
    return ResponseEntity.ok();
  }

  /**
   * 批量更新菜单
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/updateGncdList", method = RequestMethod.POST)
  public ResponseEntity updateGncdList(@RequestBody List<SysGncdEntity> GnanList) {
    for (SysGncdEntity sgge : GnanList) {
      mnuService.updateGncdList(sgge);
    }
    return ResponseEntity.ok();
  }


    /**
     * 查询 功能按钮是否显示
     */
    @RequestMapping(value = "/queryGnanByGndm", method = RequestMethod.POST)
    public ResponseEntity queryGnanByGndm(@RequestBody Map queryParams) {
        String cddm = (String) queryParams.get("cddm");
        String gndm = (String) queryParams.get("gndm");
        SysGncdGnanEntity gnan = mnuService.queryGnanByGndm(cddm, gndm);
        return ResponseEntity.ok().put("gnan", gnan);
    }
}
